package com.chilly.io;

import org.junit.jupiter.api.Test;

import java.io.*;

/**
 * 字节缓冲流
 * Created by Chilly Cui on 2020/9/24.
 */
public class BufferedByteDemo {

    //使用缓冲流读取数据(一次传输一个8M的字节数组)(最常使用)
    @Test
    void test4() throws IOException {
        long start = System.currentTimeMillis();
        BufferedInputStream fis = new BufferedInputStream(new FileInputStream("c:/env/三国/视频.mp4"));
        BufferedOutputStream fos = new BufferedOutputStream(new FileOutputStream("c:/env/三国/拷贝.mp4"));

        int len;
        byte[] data = new byte[8 * 1024];
        while ((len = fis.read(data)) != -1) {
            fos.write(data, 0, len);
        }

        System.out.println("拷贝电影耗时:{}ms");
        System.out.println(System.currentTimeMillis() - start);
        //        拷贝电影耗时:1946ms
    }

    //使用缓冲流读取数据(一次传输一个字节)
    @Test
    void test3() throws IOException {
        long start = System.currentTimeMillis();
        BufferedInputStream fis = new BufferedInputStream(new FileInputStream("D:/三国/视频.mp4"));
        BufferedOutputStream fos = new BufferedOutputStream(new FileOutputStream("D:/三国/拷贝.mp4"));

        int data;
        while ((data = fis.read()) != -1) {
            fos.write(data);
        }

//        log.info("拷贝电影耗时:{}ms", System.currentTimeMillis() - start);
//        拷贝电影耗时:39033ms
    }

    //使用基本的流读取数据(一次传输一个8M的字节数组)
    @Test
    void test2() throws IOException {
        long start = System.currentTimeMillis();
        FileInputStream fis = new FileInputStream("c:/env/三国/视频.mp4");
        FileOutputStream fos = new FileOutputStream("c:/env/三国/拷贝.mp4");

        int len;
        byte[] data = new byte[8 * 1024];
        while ((len = fis.read(data)) != -1) {
            fos.write(data, 0, len);
        }
        System.out.println("拷贝电影耗时:{}ms");
        System.out.println(System.currentTimeMillis() - start);
//        拷贝电影耗时: 4651 ms
    }

    //使用基本的流读取数据(一次传输一个字节)
    @Test
    void test1() throws IOException {
        long start = System.currentTimeMillis();
        FileInputStream fis = new FileInputStream("D:/三国/视频.mp4");
        FileOutputStream fos = new FileOutputStream("D:/三国/拷贝.mp4");

        int data;
        while ((data = fis.read()) != -1) {
            fos.write(data);
        }
//        log.info("拷贝电影耗时:{}ms", System.currentTimeMillis() - start);
//        五分钟还没拷好，关闭程序了...

    }
}
